﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class AboutUsDao:ITopLinkDao
    {
        public int Add(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objAdd = (AboutUs)obj;
                db.AddToAboutUs(objAdd);
                rs = db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Update(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objUpdate = (AboutUs)obj;
                var id = objUpdate.AboutUsID;
                var oldObj = db.AboutUs.FirstOrDefault(t => t.AboutUsID == id);
                if (oldObj != null)
                {
                    oldObj.Title = objUpdate.Title;
                    oldObj.History = objUpdate.History;
                    oldObj.Content = objUpdate.Content;
                    oldObj.PostDate = objUpdate.PostDate;
                    oldObj.AboutUsCateID = objUpdate.AboutUsCateID;
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Delete(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objDel = (AboutUs)obj;
                var id = objDel.AboutUsID;
                var oldObj = db.AboutUs.FirstOrDefault(t => t.AboutUsID == id);
                if (oldObj != null)
                {
                    db.DeleteObject(oldObj);
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public AboutUs GetByCategoryId(int categoryid)
        {
            try
            {
                using (TopLinkEntities db = new TopLinkEntities())
                {
                    return db.AboutUs.Where(q => q.AboutUsCateID == categoryid).FirstOrDefault();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
